// 忘记密码
$('.forget-pwd').click(function(){
  $('#modal-login').modal('hide');
  $('#modal-register').modal('hide');
  $('#modal-forget').modal('show');
});
// 注册
$('.go-reg').click(function(){
  $('#modal-login').modal('hide');
  $('#modal-forget').modal('hide');
  $('#modal-register').modal('show');
});
// 已有账号马上登录
$('.login_now').click(function(){
  $('#modal-login').modal('show');
  $('#modal-forget').modal('hide');
  $('#modal-register').modal('hide');
});
// 下次自动登录
$('.remember_me').click(function() {
  if ($(this).attr('src').indexOf('checkbox_selected') !== -1) {
    $(this).attr('src', 'images/checkbox.png');
  } else {
    $(this).attr('src', 'images/checkbox_selected.png');
  }
});
// 立即登录
function subLogin(){
  var phone = $('.phone');
  var password = $('.password');
  var checkbox = $('.remember');
  var phone_val = phone.val();
  var password_val = password.val();
  var checkbox_val = checkbox.prop('checked');
  var remember_val = 1;
  if(phone_val == ''){
    layer.msg('请输入手机号码',{time:1000});
    phone.focus();
    return false;
  }
  var reg = /^1\d{10}$/;
  if(!reg.test(phone_val)){
    layer.msg('手机号码格式不正确',{time:1000});
    phone.focus();
    return false;
  }
  if(password_val == ''){
    layer.msg('请输入密码',{time:1000});
    password.focus();
    return false;
  }
  if(checkbox_val == true){
    remember_val = 2;
  }
  $.ajax({
    type:'post',
    url:'/web/user/login.html',
    data:{
      phone:phone_val,
      password:password_val,
      remember:remember_val,
    },
    dataType:'json',
    success:function(res){
      if(res.code == 1001){
        layer.msg(res.msg,{time:1000});
        return false;
      }else{
        layer.msg(res.msg,{time:1000},function(){
          window.location.reload();
        });      
      }
    }
  });
}
$('#login_btn').click(function(){
  subLogin();
});
// 回车登录
$(document).keyup(function(event){
  event.stopPropagation();
  if(event.keyCode == 13){
    if($('#modal-login').hasClass('show')){
      subLogin();
    }
  }
});
(function() {
  var avatarBtn = document.getElementById('user-avatar');
  var menuList = document.getElementsByClassName('menu-list')[0];
  avatarBtn.onmouseover = function() {
    menuList.style.display = 'block';
  }
  menuList.onmouseover = function() {
    this.style.display = 'block';
  }
  menuList.onmouseout = function() {
    this.style.display = 'none';
  }
  avatarBtn.onmouseout = function() {
    menuList.style.display = 'none';
  }
})();